package com.dsw.idezjet.beans;

import java.io.Serializable;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.enterprise.context.Conversation;
import javax.enterprise.context.ConversationScoped;
import javax.inject.Inject;
import javax.inject.Named;

import com.dsw.idezjet.model.InterfaceVoos;
import com.dsw.idezjet.model.Voo;

@Named
@ConversationScoped
public class VooBean implements Serializable {

	
	private static final long serialVersionUID = 1L;
	
	@EJB
	private InterfaceVoos voos;
	
	@Inject
	private Conversation conversation;
	
	private Voo v;
	
	public VooBean(){
		this.v = new Voo();
	}
	
	@PostConstruct
	public void postConstruct(){
		this.conversation.setTimeout(60000);
	}
	
	public Voo getVoo() {
		return v;
	}

	public void setVoo(Voo v) {
		this.v = v;
	}
	
	public List<Voo> getVoos(){
		return voos.obterVoos();
	}
	
	public String cadastrarVoo(){
		voos.adicionarVoo(this.v);
		return "listarVoos?faces-redirect=true";
	}
	
	public String editarVoo(){
		voos.editarVoo(this.v);
		if ( ! conversation.isTransient() ){
			conversation.end();
		}
		return "listarVoos";
	}
	
	public String removerVoo(Voo v){
		voos.removerVoo(v);
		return "listarVoos";
	}
	
	public String prepararEdicao(Voo vooAEditar){
		if ( conversation.isTransient() ){
			this.conversation.begin();
		}
		this.v = vooAEditar;
		return "editarVoo?faces-redirect=true";
	}

}
